package com.jiamao.goplay.ui.msg;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.MsgHomeLayoutBinding;
import com.jiamao.goplay.ui.msg.vm.MsgHomeVM;
import com.jiamao.goplay.view.HorizontalItemDecoration;

import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

public class MsgHomeFragment extends BaseMVVMFragment<MsgHomeLayoutBinding, MsgHomeVM> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.msg_home_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public MsgHomeVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MsgHomeVM.class);
    }


    @Override
    public void initData() {
        super.initData();
        ImmersionBar.setStatusBarView(this, binding.getRoot().findViewById(R.id.status_bar_view));

        binding.setAdapter(new BindingRecyclerViewAdapter());

        binding.listview.addItemDecoration(new HorizontalItemDecoration(20,getContext()));//10表示10dp
        binding.listview.setItemAnimator(null);
    }


    @Override
    public void onResume() {
        super.onResume();
            viewModel.getLoadData();
        }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            viewModel.getLoadData();
        }
    }
}
